package com.onpositive.wiki3.db.catmodel.matches.classify;

import java.util.Collection;

import com.onpositive.nlp.sequence.ClassifiedSequence;
import com.onpositive.nlp.sequence.ClassifiedSequence.Token;
import com.onpositive.nlp.sequence.SequenceParser;
import com.onpositive.nlp.sequence.WikiLookup;
import com.onpositive.wiki3.db.catmodel.Category;

public class TypesOfMatcher extends AbstractRecordingMatcher {

	public TypesOfMatcher(RelationStore store) {
		super(store);
	}

	@Override
	public int accept(Category st) {
		String string = st.toString();
		if (string.equals("types_of_buses")){
			System.out.println("A");
		}
		if (string.startsWith("types_of_")) {
			string = string.substring("types_of_".length());
			ClassifiedSequence ss = SequenceParser.parse(string,
					new WikiLookup(st.toString(), st.getStore().getDb()
							.getTitleMapper()));
			Collection<Token> tokens = ss.tokens();
			if (tokens.size() == 1) {
				Token t = tokens.iterator().next();
				if (t.kind == Token.REFERENCE) {
					Category category = st.getStore().get(t.link);
					if (category == null) {
						category = st.getStore().getOrCreate(t.link);
					}
					record(RelationConstants.IS_A, category, st);
					Category[] childCategories = st.getChildCategories();
					for (Category m : childCategories) {
						record(RelationConstants.TYPE_OF, category, m);
						record(RelationConstants.IS_A, category, m);
					}
				}
			}
		}
		return 0;
	}

}
